package com.centerserver;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;

class DispathRtpPacketThread extends Thread {

	private DatagramPacketQueue mDatagramPacketQueue;
	private Map<String, ProcessRtpPacketThread> mThreadMap;
	private DatabaseManager mDatabaseManager;
	private Map<String, List<String>> mMonitorListMap;

	// private Map<String, InetAddress> mInetAddressMap;
	public DispathRtpPacketThread(DatabaseManager databaseManager,
			DatagramPacketQueue datagramPacketQueue,
			Map<String, ProcessRtpPacketThread> threadMap,
			Map<String, List<String>> monitorListMap/*
													 * , Map<String,
													 * InetAddress>
													 * inetAddressMap
													 */
	) {
		mDatabaseManager = databaseManager;
		mThreadMap = threadMap;
		// mInetAddressMap = inetAddressMap;
		mDatagramPacketQueue = datagramPacketQueue;
		mMonitorListMap = monitorListMap;
	}

	@Override
	public void run() {

		while (true) {

			DatagramPacket datagramPacket = mDatagramPacketQueue.take();

			String ipAddress = datagramPacket.getAddress().getHostAddress();

			// record the video
			mThreadMap.get(ipAddress).setRTPPacket(datagramPacket);

			/**
			 * use UDP to transfer data to local host
			 */
			String captureEndName = mDatabaseManager
					.getCaptureEndName(ipAddress);
			List<String> monitorEndList = mMonitorListMap.get(captureEndName);
			int listSize = monitorEndList.size();
			for (int i = 0; i < listSize; i++) {
				InetAddress clientAddr = null;
				try {
					clientAddr = InetAddress.getByName(monitorEndList.get(i));
				} catch (UnknownHostException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				DatagramPacket datagramPacket2 = new DatagramPacket(
						datagramPacket.getData(), datagramPacket.getLength(),
						clientAddr,
						CenterServerConfig.PORT_MONITOR_END_RECEIVE_RTP);

				DatagramSocket s = null;
				try {
					s = new DatagramSocket();
				} catch (SocketException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					s.send(datagramPacket2);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			// /**
			// * use Multicast to transfer data
			// */
			//			
			// // send RTP packet to multicast group
			// MulticastSocket s = null;
			// try {
			// s = new MulticastSocket();
			// } catch (IOException e2) {
			// e2.printStackTrace();
			// }
			// InetAddress group = mInetAddressMap.get(ipAddress);
			//			
			// DatagramPacket datagramPacket2 = new DatagramPacket(
			// datagramPacket.getData(),
			// datagramPacket.getLength(),
			// group,
			// CenterServerConfig.PORT_MONITOR_END_RECEIVE_RTP
			// );
			//			
			//			
			// try {
			// s.send(datagramPacket2);
			//				
			// // try {
			// // Thread.sleep(1000/2);
			// // } catch (InterruptedException e) {
			// // // TODO Auto-generated catch block
			// // e.printStackTrace();
			// // }
			// } catch (IOException e) {
			// System.out.println("IOException in DispatchRtpPacket");
			// e.printStackTrace();
			// }
		}
	}

}